<?php
session_start();
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$searchid = isset($_POST['searchid']) ? mysql_real_escape_string($_POST['searchid']) : '';
$offset = ($page - 1) * $rows;
$result = array();
$userType = $_SESSION["userType"];
if(!empty($_SESSION["centreName"]))
{
    $centreName = $_SESSION["centreName"];
}
include 'conn.php';
if($userType=="System Admin")
{
    $where = " ";
    $cnwhere = "OR ce.CentreName LIKE '%$searchid%'";
}
else
{
    $where = "AND ce.CentreName='$centreName'";
    $cnwhere = " ";
}
$rs = mysql_query("SELECT count(*)
FROM attendancerecord a, client c, centre ce
WHERE a.NRIC = c.NRIC AND DATE( SignInTimeStamp )=CURDATE() AND a.CentreId = ce.CentreId ".$where."
AND (
a.NRIC LIKE  '%$searchid%'
OR c.CustomerName LIKE  '%$searchid%'
OR DATE( SignInTimeStamp ) LIKE  '%$searchid%'".
    $cnwhere."
OR TIME( SignInTimeStamp ) LIKE  '%$searchid%'
OR TIME( SignOuttimeStamp ) LIKE  '%$searchid%'
OR a.Status LIKE  '%$searchid%'
)");
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];

$rs = mysql_query("SELECT a.NRIC, c.CustomerName, ce.CentreName, DATE( SignInTimeStamp ) AS Date, TIME( SignInTimeStamp ) AS TimeIn, TIME( SignOutTimeStamp ) AS TimeOut, a.AttendanceRecordID, a.Status
FROM attendancerecord a, client c, centre ce
WHERE a.NRIC = c.NRIC AND DATE( SignInTimeStamp )=CURDATE() AND a.CentreId = ce.CentreId ".$where."
AND (
a.NRIC LIKE  '%$searchid%'
OR c.CustomerName LIKE  '%$searchid%'
OR DATE( SignInTimeStamp ) LIKE  '%$searchid%'".$cnwhere."
OR TIME( SignInTimeStamp ) LIKE  '%$searchid%'
OR TIME( SignOuttimeStamp ) LIKE  '%$searchid%'
OR a.Status LIKE  '%$searchid%'
)
ORDER BY STATUS ASC  limit $offset,$rows");

$items = array();
while ($row = mysql_fetch_object($rs)) {
    array_push($items, $row);
}
$result["rows"] = $items;

echo json_encode($result);
?>